package com.ciban.smarthome.module.ui.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.ciban.smarthome.R;
import com.ciban.smarthome.module.entity.Host;

public class HostShowAdapter extends BaseAdapter {

	private Context context;
	private ArrayList<Host> list;
	private boolean isEdit = false;

	public HostShowAdapter(Context context, ArrayList<Host> list) {
		this.context = context;
		this.list = list;
	}

	public void setEdit(boolean isEdit) {
		this.isEdit = isEdit;
		notifyDataSetChanged();
	}

	public void setList(ArrayList<Host> list) {
		this.list = list;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = View.inflate(context, R.layout.listitem_host_info, null);
			holder = new ViewHolder();
			holder.tv_name = (TextView) convertView
					.findViewById(R.id.host_item_name);
			holder.tv_ip = (TextView) convertView
					.findViewById(R.id.host_item_ip);
			holder.tv_mode = (TextView) convertView
					.findViewById(R.id.host_item_mode);
			holder.cb_sel = (CheckBox) convertView
					.findViewById(R.id.host_item_sel);
			holder.tv_no = (TextView) convertView
					.findViewById(R.id.host_item_no);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		Host info = list.get(position);

		holder.tv_name.setText(info.getName());
		holder.tv_ip.setText(info.getIp());
		holder.tv_no.setText(info.getSerial_no()+"");
		holder.tv_mode.setText(info.getPort() + "");

		if (isEdit) {
			holder.cb_sel.setVisibility(View.VISIBLE);
			if (info.isSelcted())
				holder.cb_sel.setChecked(true);
			else
				holder.cb_sel.setChecked(false);
		} else {
			holder.cb_sel.setVisibility(View.GONE);
		}

		return convertView;
	}

	static class ViewHolder {
		TextView tv_name, tv_ip, tv_mode, tv_no;
		CheckBox cb_sel;
	}

}
